<?php
defined ( 'BASEPATH' ) or exit ( 'No direct script access allowed' );
class Nutrition_model extends CI_Model {
	public function __construct() {
		parent::__construct ();
	}
	/*
	 * 返回配方的数量
	 */
	public function count_nutritions(){
		$query = $this->db->get('t_nutrition');
		return $query->num_rows();
	}
	/*
	 * 根据分页信息得到配方信息
	 */
	public function get_nutritions($page,$offset){
		$this->db->limit($page, $offset);
		$this->db->order_by("id", "desc");
		$query = $this->db->get('t_nutrition');
		return $query->result_array();
	}
	/*
	 * 根据配方ID得到配方信息
	 */
	public function load($id){
		$query = $this->db->get_where('t_nutrition',array('id' => $id));
		if ($row = $query->row_array()){
			return $row;
		}
		return array();
	}
	/*
	 * 添加配方信息
	 */
	public function create($nutrition){
		$this->db->insert('t_nutrition', $nutrition);
		return $this->db->insert_id();
	}
	/*
	 * 更新配方信息
	 */
	public function update($id,$nutrition){
		$this->db->where('id', $id);
		$this->db->update('t_nutrition', $nutrition);
		return $this->db->affected_rows()>0;
	}
	/*
	 * 删除配方信息
	 */
	public function delete($id){
		$this->db->where('id', $id);
		$this->db->delete('t_nutrition');
		return $this->db->affected_rows()>0;
	}
	/*
	 * 根据配方名称返回配方信息
	 */
	public function get_by_name($name){
		$this->db->like('name', $name);
		$query=$this->db->get('t_nutrition');
		return $query->result_array();
	}
}